package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.missilike.biz.*;
import com.missilike.core.domain.EquipmentRegisterRequest;
import com.missilike.core.enums.IMPlatformType;
import com.missilike.core.enums.MilPushType;
import com.missilike.core.service.impl.JPushService;
import com.missilike.domain.request.MilPushForm;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.repository.entity.MilAccountContent;
import com.missilike.repository.entity.MilContentComment;
import com.missilike.repository.entity.MilEquipment;
import com.missilike.repository.mapper.MilEquipmentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kin
 * @since 2019-01-14
 */
@Service
public class MilEquipmentServiceImpl extends ServiceImpl<MilEquipmentMapper, MilEquipment> implements IMilEquipmentService {

    private  IMilAccountService iMilAccountService;
    private final IMilContentCommentService iMilContentCommentService;
    private final IMilAccountContentService iMilAccountContentService;
    private final JPushService jPushService;
    @Autowired
    public MilEquipmentServiceImpl(
            IMilContentCommentService iMilContentCommentService,
            IMilAccountContentService iMilAccountContentService,
            JPushService jPushService,IMilAccountService accountService
    ) {
        this.iMilContentCommentService = iMilContentCommentService;
        this.iMilAccountContentService = iMilAccountContentService;
        this.jPushService = jPushService;
        this.iMilAccountService = accountService;
    }


    /**
     * 注册机器
     * @param request
     */
    public Boolean equipmentRegister(EquipmentRegisterRequest request){
        QueryWrapper<MilEquipment> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(MilEquipment::getAccountId, request.getAccountId());
        queryWrapper.lambda().eq(MilEquipment::getPlatform, request.getPlatform());
        queryWrapper.lambda().eq(MilEquipment::getRegistrationId, request.getRegistrationId());
        MilEquipment milEquipment = baseMapper.selectOne(queryWrapper);
        if (milEquipment != null && milEquipment.getEquipmentStatus() == 0) {
            return true;
        }

        if(milEquipment == null)
        {
            milEquipment = new MilEquipment();
            milEquipment.setAccountId(request.getAccountId());
            milEquipment.setPlatform(request.getPlatform());
            milEquipment.setRegistrationId(request.getRegistrationId());
            milEquipment.setEquipmentStatus(0);
            return baseMapper.insert(milEquipment)  > 0;
        }else {
            milEquipment.setAccountId(request.getAccountId());
            milEquipment.setPlatform(request.getPlatform());
            milEquipment.setRegistrationId(request.getRegistrationId());
            milEquipment.setEquipmentStatus(0);
            return baseMapper.update(milEquipment, new UpdateWrapper<MilEquipment>()
                    .lambda()
                    .eq(MilEquipment::getId, Arrays.asList(milEquipment.getId()))) > 0;
        }
    }

    /**
     * 消息推送
     * @param milPushForm
     * @return
     */
    public Boolean publishMassage(MilPushForm milPushForm) {
        iMilAccountService.getAccount(milPushForm.getAccountId());
        String accountId = "";
        String massage = milPushForm.getMassage();
        String title = milPushForm.getTitle();
        if (milPushForm.getType() == MilPushType.SYSTEM.getValue()) {
            //系统推送
            jPushService.pushAll(massage);
        } else {
            if (milPushForm.getType() == MilPushType.COMMENT.getValue()) {
                //评论推送
                MilContentComment milContentComment = iMilContentCommentService.getById(milPushForm.getCommentId());
                MilAccountInfo milAccountInfo = iMilAccountService.getAccount(milContentComment.getAccountId());
                title = milAccountInfo.getNickName() + " 评论了您的朋友圈";
                massage = milContentComment.getComment();
                accountId = milContentComment.getAccountId();
            } else if (milPushForm.getType() == MilPushType.REWARD.getValue()) {
                //打赏推送
                MilAccountContent milAccountContent = iMilAccountContentService.getById(milPushForm.getContentId());
                MilAccountInfo milAccountInfo = iMilAccountService.getAccount(milAccountContent.getAccountId());
                title = milAccountInfo.getNickName() + " 点赞了您的朋友圈";
                massage = "您的朋友圈 " + milAccountContent.getContentTitle() + " 被用户 " + milAccountInfo.getNickName() + "点赞了";
                accountId = milAccountContent.getAccountId();
            } else if (milPushForm.getType() == MilPushType.FRIEND_APPLICATION.getValue()) {
                //好友申请推送
                MilAccountInfo milAccountInfo = iMilAccountService.getAccount(milPushForm.getAccountId());
                title = "有人想成为您的好友";
                massage = "用户 " + milAccountInfo.getNickName() + " 想申请成为您的好友";
                accountId = milPushForm.getFriendAccountId();
            } else if (milPushForm.getType() == MilPushType.FRIEND_APPLICATION_RESPONSE.getValue()) {
                //好友申请回复推送
                MilAccountInfo milAccountInfo = iMilAccountService.getAccount(milPushForm.getAccountId());
                String applyResult = milPushForm.getApplyStatus() ? "同意" : "拒绝";
                title = "好友申请结果";
                massage = "用户 " + milAccountInfo.getNickName() + " " + applyResult + " 您的好友申请";
                accountId = milPushForm.getFriendAccountId();
            }

            QueryWrapper<MilEquipment> queryWrapper = new QueryWrapper();
            queryWrapper.lambda().eq(MilEquipment::getAccountId, accountId);
            queryWrapper.lambda().eq(MilEquipment::getEquipmentStatus, 0);
            List<MilEquipment> milEquipments = baseMapper.selectList(queryWrapper);
            String finalTitle = title;
            String finalMassage = massage;
            milEquipments.forEach(x -> {
                if (x.getPlatform() == IMPlatformType.ANDROID.getValue()) {
                    //安卓平台
                    jPushService.pushAndroidByTag(x.getRegistrationId(), finalTitle, finalMassage);
                } else if (x.getPlatform() == IMPlatformType.IOS.getValue()) {
                    //IOS平台
                    jPushService.pushIosByTag(x.getRegistrationId(), finalTitle, finalMassage);
                }
            });
        }
        return false;
    }

}
